// @ts-ignore
import { Effect, Reducer, Subscription, request } from 'umi';

export interface IndexModelState {
  navILst: any;
  bannerList: [];
  songSheetList: [];
  mvList: [];
  newSongList: [];
  rankingList: any;
}

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: any;
  reducers: any;
  subscriptions: any;
}

const indexModel: IndexModelType = {
  namespace: 'index',

  state: {
    navILst: [
      { title: '音乐馆', path: '/music' },
      { title: '排行榜', path: '/music/rank' },
      { title: '歌单', path: '/music/songList' },
      { title: '主播电台', path: '/music/dj' },
      { title: '歌手', path: '/music/artist' },
      { title: '新碟上架', path: '/' },
    ],
    bannerList: [],
    songSheetList: [],
    mvList: [],
    newSongList: [],
    rankingList: [
      {
        title: '云音乐新歌榜',
        value: '0',
        list: [],
      },
      {
        title: '云音乐热歌榜',
        value: '1',
        list: [],
      },
      {
        title: '网易原创歌曲榜',
        value: '2',
        list: [],
      },
      {
        title: '云音乐飙升榜',
        value: '3',
        list: [],
      },
    ],
  },

  effects: {
    *getBannerList({ type, payload }: any, { put, call, select }: any) {
      let data = yield request('/banner?type=' + payload.type);
      yield put({
        type: 'saveBannerList',
        payload: {
          bannerList: data.banners,
        },
      });
    },
    *getSongSheetList({ type, payload }: any, { put, call, select }: any) {
      let data = yield request(
        '/top/playlist/highquality?cat=' +
          payload.cat +
          '&limit=' +
          payload.limit,
      );
      yield put({
        type: 'saveSongSheetList',
        payload: {
          songSheetList: data.playlists,
        },
      });
    },
    *getMNList({ type, payload }: any, { put, call, select }: any) {
      let data = yield request('/personalized/mv');
      yield put({
        type: 'saveMVList',
        payload: {
          mvList: data.result,
        },
      });
    },
    *getNewSongList({ type, payload }: any, { put, call, select }: any) {
      let data = yield request('/personalized/newsong');
      yield put({
        type: 'saveNewSongList',
        payload: {
          newSongList: data.result,
        },
      });
    },
    *getRankingList({ type, payload }: any, { put, call, select }: any) {
      let data = yield request('/top/list?idx=' + payload.idx);
      let result = indexModel.state.rankingList;
      if (payload.idx == '0') {
        result[0].list = data.playlist.tracks;
      } else if (payload.idx == '1') {
        result[1].list = data.playlist.tracks;
      } else if (payload.idx == '2') {
        result[2].list = data.playlist.tracks;
      } else if (payload.idx == '3') {
        result[3].list = data.playlist.tracks;
      }
      yield put({
        type: 'saveRankingList',
        payload: {
          rankingList: result,
        },
      });
    },
  },
  reducers: {
    saveBannerList(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
    saveSongSheetList(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
    saveMVList(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
    saveRankingList(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
    saveNewSongList(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {},
};

export default indexModel;
